package com.zhongge.roundrobinqueue;

/**
 * @ClassName MyCircularQueue
 * @Description TODO
 * @Author zhongge
 * @Date 2025/8/25 17:04
 * @Version 1.0
 */
@SuppressWarnings(value = {"all"})
public class MyCircularQueue {
    //属性
    private int[] value;//数组
    private int front;//队头指针
    private int rear;//队尾指针

    /**
     * 构造函数
     * @param k
     */
    public MyCircularQueue(int k) {
        this.value = new int[k];
    }

    /**
     * 入队
     * @param value 待入队的值
     * @return true 入队成功   false 入队失败
     */
    public boolean enQueue(int value) {
        //队满
        if (isFull()) {
            return false;
        }
        this.value[rear] = value;
        this.rear = (this.rear + 1) % this.value.length;
        return true;
    }

    /**
     * 出队
     * @return
     */
    public boolean deQueue() {
        //对空
        if (isEmpty()) {
            return false;
        }
        this.front = (this.front + 1) % this.value.length;
        return true;
    }

    /**
     * 获取队头元素
     * @return
     */
    public int Front() {
        if (isEmpty()) {
            return -1;
        }
        return this.value[this.front];
    }

    /**
     * 获取队尾元素
     * @return
     */
    public int Rear() {
        if (isEmpty()) {
            return -1;
        }
        return (this.rear == 0) ? this.value[this.value.length - 1] : this.value[this.rear - 1];
    }

    /**
     * 判断队列是否为空
     * @return
     */
    public boolean isEmpty() {
        return this.front == this.rear;
    }

    /**
     * 判断队列是否为满
     * @return
     */
    public boolean isFull() {
        return (this.rear + 1) % this.value.length == front ? true : false;
    }
}
